#ifndef __CCM4201S_UPGRADE__
#define __CCM4201S_UPGRADE__

#include <stdint.h>



#ifdef __cplusplus
extern "C" {
#endif

#define CCM4201S_CPU_NAME "CCM4201S"
#define CCM4201S_CMD_LEN 16
#define CCM4201S_BOOT_VERSION_LEN 36

#define CCM4201S_FW_V               "CCM4201S_FW_V"


#define CCM4201S_FW_FILE_SIZE_MAX   (1*1024*1024)
#define CCM4201S_FW_FILE_SIZE_MIN   (10*1024)


#define CCM4201S_BOOT_USB_VID 0x2fd0
#define CCM4201S_BOOT_USB_PID 0x0000


#define CCM4201S_BOOT_TRANSFER_TIMEOUT 3000

#define CCM4201S_EFLASH_PAGE_LEN   4096

#define CCM4201S_FLASH_BASE_ADDR    0x10001000
#define CCM4201S_SRAM_BASE_ADDR    0x20002000


#define CCM4201S_DOWNLOAD_DATA_LEN_MAX   32768

#define CCM4201S_MEMORY_PAGE_LEN   512
#define CCM4201S_EFLASH_PAGE_LEN   4096
#define CCM4201S_MEMORY_WRITE_LEN_MAX   0x8000

#define CCM4201S_SHA1_VERIFY_RESULT_LEN 0x14
#define CCM4201S_DISABLE_BOOTLOADER_BIN_LEN 244

#define CCM4201S_BOOTLOADER_VERIFY_TYPE 0x01
#define CCM4201S_OTHER_VERIFY_TYPE 0x02


#define CCM4201S_REQUEST_SENSE_LENGTH          0x12
#define CCM4201S_READ_CAPACITY_LENGTH          0x08

// Section 5.1: Command Block Wrapper (CBW)
struct command_block_wrapper {
    uint8_t dCBWSignature[4];
    uint32_t dCBWTag;
    uint32_t dCBWDataTransferLength;
    uint8_t bmCBWFlags;
    uint8_t bCBWLUN;
    uint8_t bCBWCBLength;
    uint8_t CBWCB[16];
};

// Section 5.2: Command Status Wrapper (CSW)
struct command_status_wrapper {
    uint8_t dCSWSignature[4];
    uint32_t dCSWTag;
    uint32_t dCSWDataResidue;
    uint8_t bCSWStatus;
};

typedef enum
{
    CCM4201S_CMD_SELF_TEST = 0xC7,
    CCM4201S_CMD_SECURITY_CODE = 0xCC,
    CCM4201S_CMD_ERASE_EFLASH = 0xD3,
    CCM4201S_CMD_DOWNLOAD_CODE_TO_EFLASH = 0xD7,
    CCM4201S_CMD_CHECK_CRC = 0xD8,
    CCM4201S_CMD_DOWNLOAD_CRC_VALUE = 0xD9,
    CCM4201S_CMD_RUN_APP = 0xE3,
    CCM4201S_CMD_READ_SN = 0xA0,
    CCM4201S_CMD_READ_EFLASH = 0xD0,
    CCM4201S_CMD_ENABLE_WRITE_PROTECT = 0xD2,
    CCM4201S_CMD_DISABLE_WRITE_PROTECT = 0xD5,
    CCM4201S_CMD_ENABLE_READ_PROTECT = 0xDA,
    CCM4201S_CMD_DISABLE_READ_PROTECT = 0xD4,
    CCM4201S_CMD_RESET = 0xCB,
    CCM4201S_CMD_READ_CRC = 0xA6,
} ccm4201s_download_cmd;
//需要注意：固件长度必须是4的整数倍，不然需要修改
int32_t ccm4201s_usb_upgrade(void *boot_usb_handle, void *upgrade_firmware);


#ifdef __cplusplus
}
#endif

#endif
